package fr.maistre.SystemeMarché.Vue;

import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import fr.maistre.SystemeMarché.Interface.Market;

public class MarketToPNG {
	public MarketToPNG(Market marché) {
		try {

			// tracé du marché en totalité
			MarketDataSet dataSetMarché = new MarketDataSet(
					marché.getReferences());
			JFreeChart chart = ChartFactory.createXYLineChart(
					"market evolution", "cycles", "$", dataSetMarché);
			ChartUtilities.saveChartAsPNG(
					new File("output/marketOverView.png"), chart, 1000, 1000);

			// tracé de chaque marketable
			for (String ref : marché.getReferences().keySet()) {

				MarketDataSet dataSetmarketable = new MarketDataSet(ref, marché
						.getReferences().get(ref).previousPrices);

				JFreeChart chartMarketable = ChartFactory.createXYLineChart(ref
 + " market evolution", "cycles",
								"$", dataSetmarketable);

				XYPlot plot = chartMarketable.getXYPlot();
				chartMarketable.setAntiAlias(true);

				plot.getRangeAxis().setRange(
						dataSetmarketable.getMinValue() - 0.5,
						dataSetmarketable.getMaxValue() + 0.5);

				final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
				renderer.setSeriesLinesVisible(0, true);
				renderer.setSeriesShapesVisible(0, false);
				plot.setRenderer(renderer);

				ChartUtilities.saveChartAsPNG(new File(
						"output/marketable/Marketable" + ref + ".png"),
						chartMarketable, 1000, 1000);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
